//
//  EMCityDataManager.m
//  sqlit3Test
//
//  Created by Eric MiAo on 2017/7/27.
//  Copyright © 2017年 Eric MiAo. All rights reserved.
//

#import "EMCityDataManager.h"
#import <sqlite3.h>

#define DBNAME    @"EMCityDataSqlDB.sqlite"
#define TABLENAME @"EMCityDataSqlTable"
#define UTF8DIC(DIC) [[NSString alloc]initWithData:[NSJSONSerialization dataWithJSONObject:(DIC) options:NSJSONWritingPrettyPrinted error:nil] encoding:NSUTF8StringEncoding]

@interface EMCityDataManager ()
{
    sqlite3 *__db;
    BOOL _success;
}
@end

@implementation EMCityDataManager

+ (id)shareManager {
    static id manager ;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        manager = [[[self class] alloc] init];
    });
    return manager;
}

- (void)getResultListFromKey:(EMCityDataManagerSearchKey)key withValue:(NSString *)value compltion:(void (^)(BOOL, NSArray *))compltion {
    NSString *keyStr;
    if (key == EMCityDataManagerSearchKeyLevel) {
        keyStr = @"level";
    } else if (key == EMCityDataManagerSearchKeyID) {
        keyStr = @"ID";
    } else if (key == EMCityDataManagerSearchKeyParentID) {
        keyStr = @"parent_id";
    } else if (key == EMCityDataManagerSearchKeyFirstChar) {
        keyStr = @"firstchar";
    }
    NSArray *array = [self searchFromDBWithKey:keyStr withValue:value];
    compltion(_success, array);
}

- (NSArray *)searchFromDBWithKey:(NSString *)key withValue:(NSString *)value {
    NSString *sqlQuery = [NSString stringWithFormat:@"SELECT * FROM EMCityDataSqlTable WHERE %@ = %@;",key,value];
    sqlite3_stmt * statement;

    NSString *database_path = [[NSBundle mainBundle] pathForResource:@"EMCityDataSqlDB" ofType:@"sqlite"];
    
    NSMutableArray *arrayM = [NSMutableArray array];
    if (sqlite3_open([database_path UTF8String], &__db) == SQLITE_OK) {
        _success = NO;
        if (sqlite3_prepare_v2(__db, [sqlQuery UTF8String], -1, &statement, nil) == SQLITE_OK) {
            _success = YES;
            while (sqlite3_step(statement) == SQLITE_ROW) {
                int ID = sqlite3_column_int(statement, 0);
                //                char *name = (char*)sqlite3_column_text(statement, 1);
                NSString *name = [[NSString alloc]initWithUTF8String:(char*)sqlite3_column_text(statement, 1)];
                int parent_id = sqlite3_column_int(statement, 2);
                int level = sqlite3_column_int(statement, 3);
                int filter_sort = sqlite3_column_int(statement, 4);
                NSString *firstchar = [[NSString alloc]initWithUTF8String:(char*)sqlite3_column_text(statement, 5)];
                int is_filter_view = sqlite3_column_int(statement, 6);

                
                NSDictionary *dic = @{
                                      @"ID":[NSString stringWithFormat:@"%d",ID],
                                      @"name":name,
                                      @"parent_id":[NSString stringWithFormat:@"%d",parent_id],
                                      @"level":[NSString stringWithFormat:@"%d",level],
                                      @"filter_sort":[NSString stringWithFormat:@"%d",filter_sort],
                                      @"firstchar":firstchar,
                                      @"is_filter_view":[NSString stringWithFormat:@"%d",is_filter_view]
                                      };
                [arrayM addObject:dic];
            }
        } else {
            _success = NO;
        }
    } else {
        _success = NO;
    }
    sqlite3_close(__db);
    return arrayM;
}

@end
